package com.example.myapplication.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.util.ToastUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {
    protected P mainPresenter;
    private Unbinder bind;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayout(), null);
        bind = ButterKnife.bind(this, view);
        mainPresenter = initPresenter();
        if (mainPresenter != null) {
            mainPresenter.bindView(this);
        }
        initView(view);
        initData();
        initListener();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();//注销
        if (mainPresenter != null) {
            mainPresenter.destroy();
            mainPresenter = null;
        }
        hideLoading();
    }

    protected abstract void initListener();

    protected abstract void initData();

    protected abstract void initView(View view);

    protected abstract P initPresenter();

    protected abstract int getLayout();



    @Override
    public void showToast(final String msg) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showToastLong(msg);
            }
        });

    }
}
